//
//  HomeViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 14-12-29.
//  Copyright (c) 2014年 RuiBen. All rights reserved.
//

#import "HomeViewController.h"
//#import "AutoAddTableViewCell.h"
#import "UIImageView+WebCache.h"
#define columns 2
#define rows 3
#define itemsPerPage 6
#define space 20
#define gridHight 200
#define gridWith 200
#define unValidIndex  -1
#define threshold 30
#define kAllFullSuperviewMask      UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

@interface HomeViewController ()
{
    NSUInteger count;
    NSArray *homeImageArray;
    
    NSMutableArray *advImageUrlArray;
    NSMutableArray *icarouselImageArray;
    NSInteger carouselCount;
    NSString *qrCodeStr;
    
    NSMutableArray *goldNameArray;
    NSMutableArray *currentGoldPriceArray;
    
    NSInteger timePriceTag;
   // NSTimer *priceTimer;
   // NSTimer * timer;
   // NSTimer *barCodeTimer;
}
@property (nonatomic, assign) BOOL wrap;
@end

@implementation HomeViewController
@synthesize wrap;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidUnload
{
   // NSLog(@"dddddddddd");
   // [self setScrollview:nil];
   // addbutton = nil;
    [super viewDidUnload];
   _carousel1 = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc
{
   // NSLog(@"erwrwrwrwrwrwrwrwr");
    //it's a good idea to set these to nil here to avoid
    //sending messages to a deallocated viewcontroller
    //this is true even if your project is using ARC, unless
    //you are targeting iOS 5 as a minimum deployment target
    _carousel1.delegate = nil;
    _carousel1.dataSource = nil;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [self initZCWScrollView];
//    if (_barCodeTimer!=nil) {
//        [_barCodeTimer invalidate];
//        _barCodeTimer=nil;
//    }
//    if (_priceTimer!=nil) {
//        [_priceTimer invalidate];
//        _priceTimer=nil;
//    }

//    if ([[Global getHomeTime] isEqualToString:@"Y"]) {
    
  
//    if (timer!=nil) {
//        [timer invalidate];
//        timer=nil;
//    }
    
    _storeNameLabel.text=[Global getStoreName];
    carouselCount=0;
    wrap=YES;
    qrCodeStr=@"";
    goldNameArray=[NSMutableArray array];
    currentGoldPriceArray=[NSMutableArray array];
   icarouselImageArray=[NSMutableArray array];
    advImageUrlArray=[NSMutableArray array];
   //[self resetDefaults];
   _pageControl1.defersCurrentPageDisplay = YES;
    
    
    [_barCodeBtn addTarget:self action:@selector(barCodeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self initCustomAdd];
    [self initScanView];
     _goldNameLabel.text=@"";
    [self requestRealTimePrice];
    [self requestAdvImage];

     
//    }

}
-(void)requestRealTimePrice
{
   
    if ([Global getLoginToken].length>0) {
       // [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
        // [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:20 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        
        [HttpRequest requestData:@"Common/GoldPriceList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    
                 //  NSLog(@"real====%@",severDataDict.dataDict);
                   // float realTimeGoldPrice=[[severDataDict.dataDict objectForKey:@"GoldPrice"] floatValue];
                   // [self setLabelBackground:realTimeGoldPrice];
                    
                    NSMutableArray *dataLineArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                    [currentGoldPriceArray removeAllObjects];
                    [goldNameArray removeAllObjects];
                    
                    for (int i=0; i<dataLineArray.count; i++) {
                        [goldNameArray addObject:[[dataLineArray objectAtIndex:i] objectForKey:@"GoldName"]];
                        [currentGoldPriceArray addObject:[[dataLineArray objectAtIndex:i] objectForKey:@"CurrentPrice"]];
                    }
//                    if (priceTimer!=nil) {
//                        [priceTimer invalidate];
//                        priceTimer=nil;
//                    }
                    timePriceTag=0;
                    if (goldNameArray.count>0) {
                   
                    _goldNameLabel.text=[goldNameArray objectAtIndex:timePriceTag];
                    }
                    if (currentGoldPriceArray.count>0) {
                    float realTimeGoldPrice=[[currentGoldPriceArray objectAtIndex:timePriceTag] floatValue];
                    
                    [self setLabelBackground:realTimeGoldPrice];
                    }
                    else
                    {
                        [self setLabelBackground:0.0];
                    }
                     if (_priceTimer==nil) {
                    _priceTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(setLabelBackgroundAndAdv) userInfo:nil repeats:YES];
                     }
                    
//                    for (int i=0; i<goldNameArray.count; i++) {
//                        NSLog(@"rrrrr===%@",[goldNameArray objectAtIndex:i]);
//                    }
                    
                }
                else
                {
                   
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
//                    UIViewController *controller=[[GlobalViewController alloc]init];
//                    [self.navigationController pushViewController:controller animated:YES];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        if (_barCodeTimer!=nil) {
                            [_barCodeTimer invalidate];
                            _barCodeTimer=nil;
                        }
                        if (_priceTimer!=nil) {
                            [_priceTimer invalidate];
                            _priceTimer=nil;
                        }

                         [Global loginViewController:self noteStr:requestResultStr];
                       // [Global loginViewController:self];
                    }

                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                // [Global showTextHud:@"请检查网络连接" view:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
    }
}
-(void)setLabelBackgroundAndAdv
{
   // NSLog(@"llllllaaaaddddvvv");
   // if (currentGoldPriceArray.count>0) {
    [self setLabelBackground:0.0];
    //}
    if (advImageUrlArray.count>0) {
     [self autoScrollCarousel];
    }
    //[self performSelector:@selector(requestRealTimePrice) withObject:self afterDelay:5.0];
    
}

-(void)initZCWScrollView
{
    CGRect tmp = {{0, 0}, {120, 100}};
    
    self.scrollNumber.numberSize =3;
    //UIImage *image = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth:10 topCapHeight:14];
    //    if (self.scrollNumber.backgroundView==nil) {
    //
    //        NSLog(@"self.scrollNumber.backgroundViewdddddd");
    //    self.scrollNumber.backgroundView = [[UIImageView alloc] init] ;
    //    [_scrollNumber.backgroundView setBackgroundColor:[UIColor clearColor]];
    //    }
   // @autoreleasepool {
        UIView *digitBackView = [[UIView alloc] initWithFrame:tmp];
        digitBackView.backgroundColor = [UIColor clearColor];
        digitBackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        digitBackView.autoresizesSubviews = YES;
        UIImage *image = [[UIImage imageNamed:@"labelBG.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:12];
        UIImageView *bgImageView = [[UIImageView alloc] initWithImage:image] ;
        bgImageView.frame = tmp;
        bgImageView.autoresizingMask = kAllFullSuperviewMask;
        [digitBackView addSubview:bgImageView];
        
        
        self.scrollNumber.digitBackgroundView = digitBackView;
        self.scrollNumber.digitColor = [UIColor colorWithRed:232.0f/255.0f green:216.0f/255.0f blue:112.0f/255.0f alpha:1.0];
        self.scrollNumber.digitFont = [UIFont systemFontOfSize:26.0];
        [self.scrollNumber didConfigFinish];
        
        
        
        
        self.scrollNumber1.numberSize =2;
        
        //    digitBackView=nil;
        //    bgImageView=nil;
        // UIImage *image1 = [[UIImage imageNamed:@"bj_numbg"] stretchableImageWithLeftCapWidth:10 topCapHeight:14];
        //    self.scrollNumber1.backgroundView = [[UIImageView alloc] init] ;
        //    [_scrollNumber1.backgroundView setBackgroundColor:[UIColor clearColor]];
        UIView *digitBackView1 = [[UIView alloc] initWithFrame:tmp];
        digitBackView1.backgroundColor = [UIColor clearColor];
        digitBackView1.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        digitBackView1.autoresizesSubviews = YES;
        UIImage *image1 = [[UIImage imageNamed:@"labelBG.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:12];
        UIImageView *bgImageView1 = [[UIImageView alloc] initWithImage:image1] ;
        bgImageView1.frame = tmp;
        bgImageView1.autoresizingMask = kAllFullSuperviewMask;
        [digitBackView1 addSubview:bgImageView1];
        //                       image1 = [[UIImage imageNamed:@"money_bg_mask"] stretchableImageWithLeftCapWidth:12 topCapHeight:12];
        //                       UIImageView *bgMaskImageView1 = [[UIImageView alloc] initWithImage:image1] ;
        //                       bgMaskImageView1.autoresizingMask = kAllFullSuperviewMask;
        //                       bgMaskImageView1.frame = tmp;
        //                       [digitBackView1 addSubview:bgMaskImageView1];
        
        self.scrollNumber1.digitBackgroundView = digitBackView1;
        self.scrollNumber1.digitColor =[UIColor colorWithRed:232.0f/255.0f green:216.0f/255.0f blue:112.0f/255.0f alpha:1.0];
        self.scrollNumber1.digitFont = [UIFont systemFontOfSize:26.0];
        [self.scrollNumber1 didConfigFinish];
        // int CC=11;
       
   // }
}
-(void)setLabelBackground:(float)goldPrice
{
    float realTimeGoldPrice=0.0f;
     if (goldNameArray.count>0) {
    timePriceTag++;
    if (timePriceTag>=goldNameArray.count) {
        timePriceTag=0;
    }
     realTimeGoldPrice =[[currentGoldPriceArray objectAtIndex:timePriceTag] floatValue];
     _goldNameLabel.text=[goldNameArray objectAtIndex:timePriceTag];
    }
    else
    {
        realTimeGoldPrice=goldPrice;
    }
    
   
    [_timePriceLable setText:[NSString stringWithFormat:@"%.2f",realTimeGoldPrice]];
    
   // [_timePriceLable setTextColor:[UIColor colorWithRed:200.0f/255.0f green:186.0f/255.0f blue:86.0f/255.0f alpha:1.0]];
    
    NSInteger priceCount = [_timePriceLable.text length];
    [_timePriceLable setColor:[UIColor blackColor] fromIndex:0 length:priceCount-2];
    [_timePriceLable setColor:[UIColor blackColor] fromIndex:priceCount-2 length:2];
    [_timePriceLable setFont:[UIFont systemFontOfSize:47] fromIndex:0 length:priceCount-2];
    [_timePriceLable setFont:[UIFont systemFontOfSize:35] fromIndex:priceCount-2 length:2];
   //    digitBackView1=nil;
//    bgImageView1=nil;
    [self.scrollNumber setNumber:[_timePriceLable.text intValue] withAnimationType:ZCWScrollNumAnimationTypeFast animationTime:0.3];
    
    float decimal11=realTimeGoldPrice-[_timePriceLable.text intValue];
    int decimalInt=decimal11*100;
     [self.scrollNumber1 setNumber:decimalInt withAnimationType:ZCWScrollNumAnimationTypeFast animationTime:0.3];

    // }
    
}


-(void)initScanView
{
    _scanViewContainer.hidden=YES;
}
-(void)barCodeBtnClick
{
     _scanViewContainer.hidden=NO;
    [self requestBarCodeImage];

}
-(void)requestBarCodeImage
{
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [HttpRequest requestBarCodeData:@"Common/CreateWxQrCode" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            [Global hideProgressHUDInternal:self.view];
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                      [Global hideProgressHUDInternal:self.view];
                    //   NSLog(@"severdatdddddd111=%@",severDataDict.dataDict);
                    [_barcodeImageView setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict objectForKey:@"QrCodeUrl"]]];
                    qrCodeStr=[severDataDict.dataDict objectForKey:@"QrCode"];
                    if (_barCodeTimer==nil) {
                        
                   
                    _barCodeTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(requestBarCodeInfo) userInfo:nil repeats:YES];
                    }
                    
                }
                else
                {
                     [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
//                    UIViewController *controller=[[GlobalViewController alloc]init];
//                    [self.navigationController pushViewController:controller animated:YES];
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        if (_barCodeTimer!=nil) {
                            [_barCodeTimer invalidate];
                            _barCodeTimer=nil;
                        }
                        if (_priceTimer!=nil) {
                            [_priceTimer invalidate];
                            _priceTimer=nil;
                        }
                        
                         [Global loginViewController:self noteStr:requestResultStr];
                      //  [Global loginViewController:self];
                    }

                }
            }
            else
            {
                  [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}
-(void)requestBarCodeInfo
{
   // NSLog(@"yyyyyyyyyy");
    if ([Global getLoginToken].length>0) {
        // [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
        [params setObject:qrCodeStr forKey:@"QrCode"];
        [HttpRequest requestBarCodeData:@"Common/GetQrCodeInf" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
           
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    //  [Global hideProgressHUDInternal:self.view];
                   //  NSLog(@"rrrrrrrrrrr=%@",severDataDict.dataDict);
                    if (_barCodeTimer!=nil) {
                        [_barCodeTimer invalidate];
                        _barCodeTimer=nil;
                    }

                    //
                    if ([[severDataDict.dataDict objectForKey:@"Subscribe"] integerValue]==0) {
                       // NSLog(@"rrrrrrrrrrr");
                         _scanViewContainer.hidden=YES;
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
//                    UIViewController *controller=[[GlobalViewController alloc]init];
//                    [self.navigationController pushViewController:controller animated:YES];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        if (_barCodeTimer!=nil) {
                            [_barCodeTimer invalidate];
                            _barCodeTimer=nil;
                        }
                        if (_priceTimer!=nil) {
                            [_priceTimer invalidate];
                            _priceTimer=nil;
                        }
                         NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                         [Global loginViewController:self noteStr:requestResultStr];
                       // [Global loginViewController:self];
                    }

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}
-(IBAction)scanCancelBtnClick:(id)sender
{
    // [self stopReading];
    if (_barCodeTimer!=nil) {
        [_barCodeTimer invalidate];
        _barCodeTimer=nil;
    }
  //  timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(autoScrollCarousel) userInfo:nil repeats:YES];
//    _priceTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(setLabelBackgroundAndAdv) userInfo:nil repeats:YES];
//    [HttpRequest cancelRequest];
    
    _scanViewContainer.hidden=YES;
}

-(void)requestAdvImage
{
    if ([Global getLoginToken].length>0) {
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
        NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
        [params setObject:[Global getLoginToken] forKey:@"Token"];
         [params setObject:@"H" forKey:@"AdvType"];
        [HttpRequest requestData:@"Common/AdvList" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                  //  NSLog(@"dddddd===%@",severDataDict.dataDict);
                        //  .dataDict);
                    [advImageUrlArray removeAllObjects];
                    NSMutableArray *advImageDataArray=[NSMutableArray  arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                    for (int i=0; i<advImageDataArray.count; i++) {
                        [advImageUrlArray addObject:[[advImageDataArray objectAtIndex:i] objectForKey:@"ImageUrl"]];
                    }
                    if (advImageUrlArray.count>0) {
                        
                    
                    [self setIcarouselImage:advImageUrlArray];
                   
                     carouselCount=0;
                    [self autoScrollCarousel];
                    }
                   // timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(autoScrollCarousel) userInfo:nil repeats:YES];
                   // [self performSelector:@selector(autoScrollTopAdview)
                         //      withObject:nil afterDelay:6];

                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    //  [Global showTextHud:requestResultStr view:self.view];
                    [Global showTextHudSub:requestResultStr view:self.view];
//                    UIViewController *controller=[[GlobalViewController alloc]init];
//                    [self.navigationController pushViewController:controller animated:YES];
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                        if (_barCodeTimer!=nil) {
                            [_barCodeTimer invalidate];
                            _barCodeTimer=nil;
                        }
                        if (_priceTimer!=nil) {
                            [_priceTimer invalidate];
                            _priceTimer=nil;
                        }
                        
                         [Global loginViewController:self noteStr:requestResultStr];
                       // [Global loginViewController:self];
                    }
                    

                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
        }];
        
    }
    
}
-(void)setIcarouselImage:(NSMutableArray *)imageArray
{
    icarouselImageArray =imageArray;
   // NSLog(@"icarouselImageArray===%@",icarouselImageArray);
     _carousel1.type = iCarouselTypeLinear;
    _carousel1.delegate=self;
    _carousel1.dataSource=self;
     _carousel1.pagingEnabled = YES;
    [_carousel1 reloadData];
    
    _pageControl1.numberOfPages = imageArray.count;
    _pageControl1.currentPage = 0;
    _pageControl1.currentPageIndicatorTintColor = [UIColor blackColor];
    _pageControl1.pageIndicatorTintColor = [UIColor grayColor];
   // _carousel1.pagingEnabled = YES;
    _carousel1.viewpointOffset=CGSizeMake(234, 0);
   
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
   // NSLog(@"6676776767676776");
    
    carouselCount=carousel.currentItemIndex;
    _pageControl1.currentPage=carousel.currentItemIndex;
    
}
-(IBAction)updatePage:(UIPageControl *)pageControl
{
    carouselCount=pageControl.currentPage;
    [_carousel1 scrollToItemAtIndex:pageControl.currentPage animated:YES];
}

-(void)initCustomAdd
{
   // NSArray *buttonArray=[NSArray arrayWithObjects:@"会员",@"退货",@"购物车",@"库存查询", @"订单管理",@"产品陈列",@"服务", @"要货管理", nil];
    
    NSArray *buttonArray=[NSArray arrayWithObjects:@"会员",@"购物车",@"订单",@"库存查询",@"服务",@"产品陈列", @"退货", @"到货管理", nil];

    NSArray *colorArray=[NSArray arrayWithObjects:@"#00274d",@"#002357",@"#004567",@"#00523f", @"#254900",@"#514c00",@"#4f0011",@"#542600",nil];
    NSArray *imageArray=[NSArray arrayWithObjects:@" SYhuiyuan_.png",@"SYgouwuche_.png",@"SYdingdan_.png",@"SYkucun_.png",@"SYfuwudengji_.png",@"SYchanpin_.png",@"SYtuihuanhuo_.png",@"SYdaohuo_.png",nil];
   
   
    for (UIView *v in _rightScrollView.subviews) {
        [v removeFromSuperview];
    }
    float scrollViewHeight=0.0f;
    for (int i=0; i<buttonArray.count; i++) {
        UIButton *imageButton=[UIButton buttonWithType:UIButtonTypeCustom];
        CGRect rect=imageButton.frame;
        imageButton.tag=i+1;
        // imageButton.titleLabel.text=[buttonArray objectAtIndex:i] ;
        // [imageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal] ;
        [imageButton setAlpha:0.88];
        [imageButton setBackgroundColor:[Global parseColor:[colorArray objectAtIndex:i]]];
        [imageButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        rect.size.height=165;
        rect.size.width=220;
        rect.origin.x=5+(220+5)*(i%2);
        rect.origin.y=(165+5)*(i/2);
        imageButton.frame=rect;
        
        UILabel *lineLabel=[[UILabel alloc] init];
        [lineLabel setBackgroundColor:[UIColor whiteColor]];
        [lineLabel setFrame:CGRectMake(0 , (imageButton.frame.size.height+imageButton.frame.origin.y), _rightScrollView.frame.size.width, 5)];
        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(imageButton.center.x-20, imageButton.center.y-30, 40, 40)];
        [img setImage:[UIImage imageNamed:[imageArray objectAtIndex:i]]];
        
        UILabel *titleLabel=[[UILabel alloc] init];
        titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
        // lineLabel.tag=9;
        titleLabel.text=[buttonArray objectAtIndex:i];
        titleLabel.textColor=[UIColor whiteColor];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        [titleLabel setFrame:CGRectMake(imageButton.center.x-45, img.frame.origin.y+38, 90, 22)];
        //  [self resizeLabel:titleLabel];
        
        
        
        
        [_rightScrollView addSubview:imageButton];
        [_rightScrollView addSubview:titleLabel];
        [_rightScrollView addSubview:lineLabel];
        [_rightScrollView addSubview:img];
        scrollViewHeight=rect.origin.y+rect.size.height;
        // NSLog(@"rect.origin.x====%f",rect.origin.x);
        // NSLog(@"rect.origin.y====%f",rect.origin.y);
        
    }
    for (int i=0; i<3; i++) {
        UILabel *lineLabel=[[UILabel alloc] init];
        [lineLabel setBackgroundColor:[UIColor whiteColor]];
        [lineLabel setFrame:CGRectMake(225*(i%3),0, 5, scrollViewHeight)];
        //   NSLog(@"lineLabel.y===%f",lineLabel.frame.origin.y);
        [_rightScrollView addSubview:lineLabel];
        // NSLog(@"lineLabel.x===%f",lineLabel.frame.origin.x);
    }
    [_rightScrollView setContentSize:CGSizeMake(0, scrollViewHeight)];
    [_rightScrollView setScrollEnabled:YES];
  }
- (void)viewDidLoad
{
    [super viewDidLoad];
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo)];
//    [self.alphaView addGestureRecognizer:tapGesture];
}
#pragma mark -
#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel
    return icarouselImageArray.count;
}
- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset
{
    view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);
  
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = _carousel1.perspective;
    transform = CATransform3DRotate(transform, M_PI / 6.0, 0, 1.0, 0);
    
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * _carousel1.itemWidth);
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
       // if (icarouselImageArray.count>0) {
      //  NSLog(@"99999999999");
       
        UIImageView *bulletinImageView=[[UIImageView alloc] initWithFrame: CGRectMake(0, 0,452,675)];
        [bulletinImageView setImageWithURL:[NSURL URLWithString:[icarouselImageArray objectAtIndex:index]]];
        UIView *view = [[UIView alloc] init];
        [view addSubview:bulletinImageView];
        
        bulletinImageView=nil;
        view.frame = CGRectMake(0, 0, 452,675);
        return view;
    //    }
//        else
//            return nil;
  }
    else
    {
        //get a reference to the label in the recycled view
        label = (UILabel *)[view viewWithTag:1];
    }
    
    //set background color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    view.backgroundColor = [UIColor colorWithRed:red
                                           green:green
                                            blue:blue
                                           alpha:1.0];
     label.text = @"1";
    
    return view;
}
- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    // return self.wrap;
    //customize carousel display
    //return self.wrap;
   
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value ;
        }
        case iCarouselOptionFadeMax:
        {
//            if (self.carousel.type == iCarouselTypeCustom)
//            {
//                //set opacity based on distance from camera
//                return 0.0f;
//            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}
- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
   
    
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * _carousel1.itemWidth);
}

-(void)btnClick:(UIButton *)sender
{
     [self.delegate customBtnClick:sender.tag];
}
-(void)autoScrollCarousel {
//    NSLog(@"dddd243252553");
    [_carousel1 scrollToItemAtIndex:carouselCount animated:YES];
    if(carouselCount== icarouselImageArray.count){
        carouselCount=0;
    }else{
        carouselCount++;
    }

}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
